package com.blood.modules.system.controller;

import com.blood.core.annotation.JwtIgnore;
import com.blood.core.resp.ResponseResult;
import com.blood.core.util.ObjectAssertUtil;
import com.blood.modules.system.dto.req.CheckAppVersionReq;
import com.blood.modules.system.dto.resp.CheckAppVersionResp;
import com.blood.modules.system.dto.resp.UploadResp;
import com.blood.modules.system.service.IFTPService;
import com.blood.modules.system.service.ISysService;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author yelong
 * @version 1.0
 * @date 2020/8/14 14:51
 * @description
 */
@Slf4j
@Api(value = "系统模块", tags = "系统模块")
@RestController
@RequestMapping("/sys/api")
@ApiSort(999)
public class SysController {

    @Autowired
    private ISysService sysService;

    @Autowired
    private IFTPService ftpService;

    /**
     * 检查app版本
     *
     * @param req
     * @return
     */
    @PostMapping("/checkAppVersion")
    @ApiOperation(value = "检查app版本")
    @JwtIgnore
    public ResponseResult<CheckAppVersionResp> checkAppVersion(@RequestBody CheckAppVersionReq req) {
        ObjectAssertUtil.isBoolean(null==req.getVersion(), "没有指定的版本号!");
        ObjectAssertUtil.stringIsNull(req.getServiceType(), "没有指定服务端类型!");
        ObjectAssertUtil.isBoolean(null == req.getType(), "没有指定版本类型!");
        return sysService.checkAppVersion(req);
    }


    @PostMapping("/upload")
    @ApiOperation(value = "文件上传")
    @JwtIgnore
    public ResponseResult<UploadResp> upload(@RequestParam("file") MultipartFile file) {
        ObjectAssertUtil.isBoolean(file.isEmpty(), "文件不能为空!");
        return ftpService.upload(file);
    }

    @PostMapping("/batchUpload")
    @ApiOperation(value = "批量文件上传")
    @JwtIgnore
    public ResponseResult<List<UploadResp>> batchUpload(@RequestParam("files") List<MultipartFile> files) {
        ObjectAssertUtil.isBoolean(CollectionUtils.isEmpty(files), "文件不能为空!");
        return ftpService.batchUpload(files);
    }
}
